﻿// Load the SDK asynchronously
(function (d, s, id) {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) return;
    js = d.createElement(s); js.id = id;
    js.src = "//connect.facebook.net/vi_VN/sdk.js";
    fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

window.fbAsyncInit = function () {
    FB.init({
        appId: //'1506413802911728', //Test
               '1506413422911766', // Real
        cookie: true,  // enable cookies to allow the server to access 
        // the session
        xfbml: true,  // parse social plugins on this page
        version: 'v2.0', // use version 2.0
    });
    
    FB.getLoginStatus(function (response) {
        statusChangeCallback(response);
    });

    FB.Event.subscribe('auth.statusChange', function (response) {
        if (response.status === 'connected') { //completed login process
            showUserInfo();
        }
        else { //completed logout process
            hideUserInfo();
        }
    });


    // This is called with the results from from FB.getLoginStatus().
    function statusChangeCallback(response) {
        if (response.status === 'connected') {
            // Logged into your app and Facebook.
            showUserInfo();
        } else if (response.status === 'not_authorized') {
            // The person is logged into Facebook, but not your app.
            //document.getElementById('status').innerHTML = 'Please log ' + 'into this app.';
        } else {
            // The person is not logged into Facebook, so we're not sure if
            // they are logged into this app or not.
            //document.getElementById('status').innerHTML = 'Please log ' + 'into Facebook.';
        }
    }

    function showUserInfo() {
        FB.api('/me?fields=id,name,picture', function (response) {
            //var info = JSON.stringify(response);
            var userId = response.id;
            var userName = response.name;
            var pictureUrl = response.picture.data.url;

            if (pictureUrl != null) {
                $(".ft-fb-info").html(
                    "<img src='" + pictureUrl + "' style='height:16px;' />&nbsp;" +
                    "<span>" + userName + "</span>"
                    );
                $(".ft-fb-info").show();
                $(".facebook-account-scope").show();
                $(".footy-account-scope").hide();
            }
        });
    }

    function hideUserInfo() {
        $(".ft-fb-info").hide();
        $(".footy-account-scope").show();
    }
}


